package com.maxverse.boot.web2image.queue.consumer;

import com.maxverse.boot.web2image.common.model.TaskMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class TaskConsumer {
    private static final String TASK_QUEUE_KEY = "web2image:task:queue";
    private static final long TIMEOUT = 0; // 0表示永久等待
    
    @Autowired
    private RedisTemplate<String, TaskMessage> taskRedisTemplate;
    
    public TaskMessage receiveTask() {
        try {
            // 从队列左端弹出一个任务，如果队列为空则等待
            return taskRedisTemplate.opsForList().leftPop(TASK_QUEUE_KEY, TIMEOUT, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("Failed to receive task from queue", e);
            return null;
        }
    }
} 